package cn.edu.scau.cmi.chenjia.clientFxml;

import java.net.URL;
import java.util.ResourceBundle;

import cn.edu.scau.cmi.chenjia.adapter.Student;
import cn.edu.scau.cmi.chenjia.adapter.Target;
import cn.edu.scau.cmi.chenjia.adapter.objectAdapter;
import cn.edu.scau.cmi.chenjia.composite.consistent.ConsistentOrganization;
import cn.edu.scau.cmi.chenjia.composite.consistent.ConsistentPerson;
import cn.edu.scau.cmi.chenjia.composite.consistent.ConsistentTeam;
import javafx.fxml.Initializable;

import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class ConsistentOrganizationUIController implements Initializable{
	@FXML
	private TreeView<String> Tree1;
	@FXML
	private  ContextMenu addMenu;
	@FXML
	private  MenuItem addMenuItem;
	@FXML
	private  MenuItem addMenuItem1;
	@FXML
	private TextArea text;
	
	List<ConsistentOrganization> employees=new ArrayList();
	ConsistentTeam st=new ConsistentTeam("Ethan Williams", "Sales Department");
	public ConsistentOrganizationUIController() {
		display();
	}
	
	public void display() {
		ConsistentPerson str1=new ConsistentPerson("1");
		ConsistentPerson str2=new ConsistentPerson("2");
		ConsistentPerson str3=new ConsistentPerson("3");
		
		st.addChild(str1);
		st.addChild(str2);
		st.addChild(str3);
		
		
		 employees.add(st);
		 employees.add( new ConsistentTeam("Emma Jones", "Sales Department"));
		 employees.add( new ConsistentTeam("Michael Brown", "Sales Department"));
		 employees.add(new ConsistentTeam("Anna Black", "Sales Department"));
		 employees.add(new ConsistentTeam("Rodger York", "Sales Department"));
		 employees.add(new ConsistentTeam("Susan Collins", "Sales Department"));
		 employees.add(new ConsistentTeam("Mike Graham", "IT Support"));
		 employees.add(new ConsistentTeam("Judy Mayer", "IT Support"));
		 employees.add(new ConsistentTeam("Gregory Smith", "IT Support"));
		 employees.add(new ConsistentPerson("Jacob Smith"));
		 employees.add(new ConsistentPerson("Isabella Johnson"));

	}
	
	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		// TODO Auto-generated method stub
		
				
		 TreeItem<String> rootNode = 
			        new TreeItem<String>("MyCompany Human Resources");
		 
		 String str;
		 rootNode.setExpanded(true);
		 for (ConsistentOrganization employee : employees) {
	            TreeItem<String> empLeaf = new TreeItem<String>(employee.getName());
	            boolean found = false;
	            for (TreeItem<String> depNode : rootNode.getChildren()) {
	                if (depNode.getValue().contentEquals(employee.getDepartment())){
	                    depNode.getChildren().add(empLeaf);
	                    depNode.setExpanded(true);
	                    found = true;
	                    break;
	                }
	            }
	            if (!found) {
	            	if(employee.getDepartment()!="SafePerson") {
	                TreeItem<String> depNode = new TreeItem<String>(
	                    employee.getDepartment()
	                );
	                if(st.getDepartment()==employee.getDepartment()) {
	                	TreeItem<String> NextNode=new TreeItem<String>(employee.getName());
	                for(int i=0;i<st.getChildren().size();i++) {
	                	TreeItem<String> nextNode=new TreeItem<String>(st.getChildren().get(i).getName());
	                	NextNode.getChildren().add(nextNode);
	                	NextNode.setExpanded(true);
	                	
	                }
	                depNode.getChildren().add(NextNode);
	                rootNode.getChildren().add(depNode);
	                }
	                else {
	                	rootNode.getChildren().add(depNode);
	                depNode.getChildren().add(empLeaf);
	                }
	                }
	            	else if(employee.getDepartment()=="ConsistentPerson"){
	            		rootNode.getChildren().add(empLeaf);
	            	}
	            }
	        }
		 Tree1.setRoot(rootNode);
		 Tree1.setEditable(true);

	}
	
	
	public void eventAction(){
		TreeItem tempTreeItem = (TreeItem)Tree1.getSelectionModel().getSelectedItem(); 
		if(!tempTreeItem.isLeaf()) {
			ConsistentPerson sp=new ConsistentPerson("个人");
			employees.add(sp);
		 TreeItem newEmployee = new TreeItem<String>(sp.getName());
         tempTreeItem.getChildren().add(newEmployee);
         text.appendText(sp.doSomething()+'\n');
                    }	
		else {
			ConsistentPerson sp=new ConsistentPerson("个人");
			ConsistentPerson sp1=new ConsistentPerson("个人");
			text.appendText((sp.addChild(sp1)).getDepartment()+'\n');
		}
	}
	
	public void eventAction1(){
		TreeItem tempTreeItem = (TreeItem)Tree1.getSelectionModel().getSelectedItem(); 
		if(!tempTreeItem.isLeaf()) {
			
			ConsistentPerson sp=new ConsistentPerson("个人");
			ConsistentTeam tp=new ConsistentTeam("个人(团队)","团队");
			tp.addChild(sp);
			employees.add(tp);
		 TreeItem nextNode = new TreeItem<String>(tp.getChildren().get(0).getDepartment());
		 TreeItem rootNode = new TreeItem<String>(tp.getDepartment());
		 TreeItem depNode = new TreeItem<String>(tp.getName());
		 rootNode.getChildren().add(depNode);
		 rootNode.getChildren().add(nextNode);
		 rootNode.setExpanded(true);
		 tempTreeItem.getChildren().add(rootNode);
		 text.appendText(tp.doSomething()+'\n'+"并加入一致型个人"+'\n');
                    }
		else {
			ConsistentPerson sp=new ConsistentPerson("个人");
			ConsistentPerson sp1=new ConsistentPerson("个人");
			text.appendText((sp.addChild(sp1)).getDepartment()+'\n');
		}
	}
	
	
	
}
